package com.bing.utils;

import com.bing.bean.Tpp.tuling.*;
import com.bing.bean.weixin.RequestMessage;
import com.bing.bean.weixin.ResponseMessage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/6.
 */
public class Tuling {


    private static final Logger logger = LoggerFactory.getLogger(Tuling.class);
    public static final String key = "51ac0b96beb2b2db6cc96cf59be27010";
    public static final String url = "http://www.tuling123.com/openapi/api";

    public static ResponseMessage talk(RequestMessage requestMessage) {
        Map<String, String> data = new HashMap<>();
        data.put("key", key);
        data.put("info", requestMessage.getContent());
        data.put("userid", requestMessage.getFromUserName());
        try {
            Document document = Jsoup.connect(url).data(data).header("Content-type", "text/html; charset=utf-8").ignoreContentType(true).get();
            logger.info("图灵机器返回内容:{}", document.text());
            Map map = CommonUtils.GSON.fromJson(document.text(), Map.class);
            switch (((Double) map.get("code")).intValue()) {
                //文本类数据
                case 100000:
                    Text text = CommonUtils.GSON.fromJson(document.text(), Text.class);
                    return text.toMsg(requestMessage);
                //列车数据
                case 305000:
                    Trains trains = CommonUtils.GSON.fromJson(document.text(), Trains.class);
                    return trains.toMsg(requestMessage);
                //航班数据
                case 306000:
                    Flights flights = CommonUtils.GSON.fromJson(document.text(), Flights.class);
                    return flights.toMsg(requestMessage);
                //网址类数据
                case 200000:
                    Link link = CommonUtils.GSON.fromJson(document.text(), Link.class);
                    return link.toMsg(requestMessage);
                //新闻类数据
                case 302000:
                    News news = CommonUtils.GSON.fromJson(document.text(), News.class);
                    return news.toMsg(requestMessage);
                //菜谱、视频、小说
                case 308000:
                    CaiPus caiPus = CommonUtils.GSON.fromJson(document.text(), CaiPus.class);
                    return caiPus.toMsg(requestMessage);
                case 40002:
                    logger.info("请求内容为空");
                    return MsgHelper.toText(requestMessage, "有什么可以帮助您的呢?");
            }
        }catch (IOException e){
            return MsgHelper.toText(requestMessage, "机器人发生了点小故障,请稍后再试");

        }
        return MsgHelper.toText(requestMessage, "有什么可以帮助您的呢?");
    }

}
